package org.mysfit.ctp4j.core;

import org.mysfit.ctp4j.api.vo.QuoteInsertVO;
import org.mysfit.ctp4j.core.service.impl.TradeHelper;
import org.mysfit.ctp4j.infra.pojo.CtpUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ctp.thosttraderapi.CThostFtdcInputForQuoteField;
import ctp.thosttraderapi.CThostFtdcInputOrderField;
import ctp.thosttraderapi.CThostFtdcInputQuoteField;
import ctp.thosttraderapi.CThostFtdcOrderField;
import ctp.thosttraderapi.CThostFtdcQuoteField;
import ctp.thosttraderapi.CThostFtdcReqAuthenticateField;
import ctp.thosttraderapi.CThostFtdcReqUserLoginField;
import ctp.thosttraderapi.CThostFtdcRspAuthenticateField;
import ctp.thosttraderapi.CThostFtdcRspInfoField;
import ctp.thosttraderapi.CThostFtdcRspUserLoginField;
import ctp.thosttraderapi.CThostFtdcSettlementInfoConfirmField;
import ctp.thosttraderapi.CThostFtdcTradeField;
import ctp.thosttraderapi.CThostFtdcTraderApi;
import ctp.thosttraderapi.CThostFtdcTraderSpi;
import ctp.thosttraderapi.CThostFtdcUserLogoutField;

/**
 * 
 * @author long.gf
 * @since 2022-12-06
 *
 */
public class MyCThostFtdcTraderHelperSpi extends CThostFtdcTraderSpi {
	private static final Logger LOGGER = LoggerFactory.getLogger(MyCThostFtdcTraderHelperSpi.class);
	
    private CtpUser ctpUser;
    private CThostFtdcTraderApi traderApi;
    private TradeHelper tradeHelper;
    
    public MyCThostFtdcTraderHelperSpi(CtpUser ctpUser, 
    		CThostFtdcTraderApi traderApi, 
    		TradeHelper tradeHelper ) 
    {
        this.ctpUser = ctpUser;
        this.traderApi = traderApi;
        this.tradeHelper = tradeHelper;
    }
    
    public MyCThostFtdcTraderHelperSpi(CtpUser ctpUser, 
    		CThostFtdcTraderApi traderApi, 
    		TradeHelper tradeHelper,
    		QuoteInsertVO quoteInsertVO) 
    {
        this.ctpUser = ctpUser;
        this.traderApi = traderApi;
        this.tradeHelper = tradeHelper;
    }

	@Override
	public void OnFrontConnected() {
		LOGGER.info("OnFrontConnected");
		int requestId = (int) (Math.random() * 10000);
		//身份验证
		CThostFtdcReqAuthenticateField pReqAuthenticateField = new CThostFtdcReqAuthenticateField();
		pReqAuthenticateField.setBrokerID(this.ctpUser.getBrokerId());
		pReqAuthenticateField.setUserID(this.ctpUser.getInvestorId());
		pReqAuthenticateField.setAppID("simnow_client_test");
		pReqAuthenticateField.setAuthCode("0000000000000000");
		traderApi.ReqAuthenticate(pReqAuthenticateField, requestId);
	}

	@Override
	public void OnFrontDisconnected(int nReason) {
		LOGGER.info("OnFrontDisconnected");
		LOGGER.error("前置服务断开连接, 返回错误码:{}", nReason);
		super.OnFrontDisconnected(nReason);
	}

	@Override
	public void OnRspAuthenticate(CThostFtdcRspAuthenticateField pRspAuthenticateField, CThostFtdcRspInfoField pRspInfo, int nRequestID,
			boolean bIsLast) {
		LOGGER.info("OnRspAuthenticate");
		LOGGER.error("用户身份认证返回结果--> nRequestID:[{}], ErrorID:[{}], ErrorMsg:[{}]", nRequestID, pRspInfo.getErrorID(), pRspInfo.getErrorMsg());
		// 登录
		CThostFtdcReqUserLoginField pReqUserLoginField = new CThostFtdcReqUserLoginField(); 
		pReqUserLoginField.setBrokerID(this.ctpUser.getBrokerId());
		pReqUserLoginField.setUserID(this.ctpUser.getInvestorId());
		pReqUserLoginField.setPassword(this.ctpUser.getPassword());
        traderApi.ReqUserLogin(pReqUserLoginField, nRequestID);
	}

	@Override
	public void OnRspUserLogin(CThostFtdcRspUserLoginField pRspUserLogin, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
		LOGGER.info("OnRspUserLogin");
		LOGGER.info("登录返回结果--> nRequestID:[{}], ErrorID:[{}], ErrorMsg:[{}]", nRequestID, pRspInfo.getErrorID(), pRspInfo.getErrorMsg());
		
		if(pRspInfo.getErrorID() != 0) {
			super.OnRspUserLogin(pRspUserLogin, pRspInfo, nRequestID, bIsLast);
			return;
		}
		// 确认结算结果
		CThostFtdcSettlementInfoConfirmField pSettlementInfoConfirm = new CThostFtdcSettlementInfoConfirmField();
		pSettlementInfoConfirm.setBrokerID(this.ctpUser.getBrokerId());
		pSettlementInfoConfirm.setInvestorID(this.ctpUser.getInvestorId());
		traderApi.ReqSettlementInfoConfirm(pSettlementInfoConfirm, nRequestID);
	}

	@Override
	public void OnRspUserLogout(CThostFtdcUserLogoutField pUserLogout, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
		LOGGER.info("OnRspUserLogout");
		super.OnRspUserLogout(pUserLogout, pRspInfo, nRequestID, bIsLast);
	}
	
	@Override
	public void OnRspSettlementInfoConfirm(CThostFtdcSettlementInfoConfirmField pSettlementInfoConfirm, CThostFtdcRspInfoField pRspInfo,
			int nRequestID, boolean bIsLast) {
		LOGGER.info("OnRspSettlementInfoConfirm");
		LOGGER.error("确认结算单返回--> nRequestID:[{}], ErrorID:[{}], ErrorMsg:[{}]", nRequestID, pRspInfo.getErrorID(), pRspInfo.getErrorMsg());
	}
	
	@Override
	public void OnRspOrderInsert(CThostFtdcInputOrderField pInputOrder, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
		LOGGER.info("OnRspOrderInsert");
		LOGGER.error("报单返回--> nRequestID:[{}], ErrorID:[{}], ErrorMsg:[{}]", nRequestID, pRspInfo.getErrorID(), pRspInfo.getErrorMsg());
	}

	@Override
	public void OnErrRtnOrderInsert(CThostFtdcInputOrderField pInputOrder, CThostFtdcRspInfoField pRspInfo) {
		LOGGER.info("OnErrRtnOrderInsert");
		LOGGER.error("报单失败--> RequestID:[{}], ErrorID:[{}], ErrorMsg:[{}]", pInputOrder.getRequestID(), pRspInfo.getErrorID(), pRspInfo.getErrorMsg());
	}
	
	@Override
	public void OnRspQuoteInsert(CThostFtdcInputQuoteField pInputQuote, CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
		LOGGER.info("OnRspQuoteInsert");
		LOGGER.error("报价返回--> nRequestID:[{}], ErrorID:[{}], ErrorMsg:[{}]", nRequestID, pRspInfo.getErrorID(), pRspInfo.getErrorMsg());
	}
	
	@Override
	public void OnErrRtnForQuoteInsert(CThostFtdcInputForQuoteField pInputForQuote, CThostFtdcRspInfoField pRspInfo) {
		LOGGER.info("OnErrRtnForQuoteInsert");
		LOGGER.error("报价失败--> ForQuoteRef:[{}], ErrorID:[{}], ErrorMsg:[{}]", pInputForQuote.getForQuoteRef(), pRspInfo.getErrorID(), pRspInfo.getErrorMsg());
	}
	 
	@Override
	public void OnRtnOrder(CThostFtdcOrderField pOrder) {
		LOGGER.info("OnRtnOrder");
		LOGGER.info("报单成功通知--> OrderSysID:[{}],  OrderLocalID:[{}],OrdersStatus:[{}], reqID:[{}]",pOrder.getOrderSysID(), pOrder.getOrderLocalID(),pOrder.getOrderStatus(), pOrder.getRequestID());
		super.OnRtnOrder(pOrder);
	}
	
	@Override
	public void OnRtnQuote(CThostFtdcQuoteField pQuote) {
		LOGGER.info("OnRtnQuote");
		LOGGER.info("报价成功通知--> QuoteSysID:[{}], reqID:[{}]", pQuote.getQuoteSysID(),pQuote.getRequestID());
		super.OnRtnQuote(pQuote);
	}

	@Override
	public void OnRtnTrade(CThostFtdcTradeField pTrade) {
		LOGGER.info("OnRtnTrade");
		LOGGER.info("成交通知--> TradeID:[{}]", pTrade.getTradeID());
		super.OnRtnTrade(pTrade);
	}

	@Override
	public void OnRspError(CThostFtdcRspInfoField pRspInfo, int nRequestID, boolean bIsLast) {
		LOGGER.info("OnRspError");
		super.OnRspError(pRspInfo, nRequestID, bIsLast);
	}
	
}
